#!/usr/bin/env python3
# coding: utf-8

from lab4 import TuringMachine as _TM


kk = [
        # 0 - go thru '0's
        {
            '0':(1, '0', 'r'),
            '1':(3, '1', 'r'),
            '-':(0, '-', 'r'),
            ' ':(9, ' ', 'l')   # cannot continue
        },

        # 1 - seek and remove a '0'
        {
            '0':(0, '-', 'r'),
            '1':(3, '1', 'r'),  # read '1', jump to seeking and removing a '1'
            '-':(1, '-', 'r'),  # continue seeking
            ' ':(?, ' ', 'l')
        },

        # 2 - '1's
        {
            '0':(9, '0', 'n'),  # return False
            '1':(3, '1', 'r'),
            '-':(2, '-', 'r'),
            ' ':(?, ' ', 'l')
        },

        # 3 - seek and destroy '1'
        {
            '0':(9, '0', 'n'),  # return False
            '1':(2, '-', 'r'),
            '-':(3, '-', 'r'),
            ' ':(?, ' ', 'n')   # go back
        },

        # 4 - backtracking - start - even
        {
            '0':(7, '0', 'l'),
            '1':(7, '1', 'l'),
            '-':(4, '-', 'l'),
            ' ':(?, ' ', 'n')
        },

        # 5 - backtracking - start - odd
        {
            '0':(6, '0', 'l'),
            '1':(6, '1', 'l'),
            '-':(5, '-', 'l'),
            ' ':(?, ' ', 'n')
        },

        # 6 - backtracking - odd
        {
            '0':(7, '0', 'l'),
            '1':(7, '1', 'l'),
            '-':(6, '-', 'l'),
            ' ':(0, ' ', 'r')  # continue checking
        },

        # 7 - backtracking - odd
        {
            '0':(6, '0', 'l'),
            '1':(6, '1', 'l'),
            '-':(7, '-', 'l'),
            ' ':(9, ' ', 'n')  # reject
        }
     ]

class TMStep(_TM):
    """
    hello docstring
    """

    def start(self, w):
        self.tot_steps = 0
        return _TM.start(self, w)

    def step(self):
        self.tot_steps += 1
        return _TM.step(self)

    def __str__(self):
        return _TM.__str__(self) + \
               ' (tot steps {})'.format(self.tot_steps)


if __name__ == "__main__":
    m = TMStep(kk)
    for w in ('0' * 20 + '1' * 20,
              '0' * 50 + '1' * 60,
              '0' * 32 + '1' * 64,
              '001',
              '000011',
              '0000111'):
        print(m.run(w, True))
        print(m)
